/**
 * 
 */
package ga.nqueens;

import java.util.List;

import org.jheuristics.Individual;
import org.jheuristics.ga.DoubleFitness;
import org.jheuristics.ga.Fitness;
import org.jheuristics.ga.FitnessEvaluator;
import org.jheuristics.ga.GAConfig;
import org.jheuristics.ga.GAStatus;
import org.jheuristics.ga.Population;

/**
 * @author thiago
 *
 */
public class NQueensFitnessEvaluator implements FitnessEvaluator {

	/* (non-Javadoc)
	 * @see org.jheuristics.ga.FitnessEvaluator#evaluateFitness(org.jheuristics.Individual, org.jheuristics.ga.Population, org.jheuristics.ga.GAStatus, org.jheuristics.ga.GAConfig)
	 */
	public Fitness evaluateFitness(Individual individual, Population population, GAStatus status, GAConfig config) {
		
		if (null == individual) {
			throw new NullPointerException();
		}
		
		List<Integer> gens = (List<Integer>) individual.getGens();
		
		Board board = new Board(gens.size());
		
		for (int i = 0; i < gens.size(); i++) {
			board.addRainha(i, gens.get(i));
		}
		
		final int cost = board.hits();
		
		return new DoubleFitness(){

			/* (non-Javadoc)
			 * @see org.jheuristics.ga.DoubleFitness#toDouble()
			 */
			public double toDouble() {
				return -cost;
			}

			/**
			 * @param o
			 * @return
			 */
			public int compareTo(Object o) {
				return (int) (toDouble() - ((DoubleFitness)o).toDouble());
			}
			
		};
		
	}

}
